package com.fulicarpet.shop.application;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.alibaba.security.rp.RPSDK;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.fulicarpet.shop.config.Constants;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.fulicarpet.shop.utils.CrashHandler;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;

/**
 * Created by admin on 2018/7/4.
 */

public class MyApplication extends Application{
    private static MyApplication instance;
    private static Context context;

    public MyApplication() {
    }

    /**
     * 单例模式，返回一个application实例
     * @return
     */
    public static MyApplication getInstance() {
        if (instance == null) {
            instance=new MyApplication();
        }
        return instance;
    }

    public static Context getContext() {
        return context;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        CrashHandler.getInstance().init(context);
        initOKHttp();
        ZXingLibrary.initDisplayOpinion(this);
        //极光
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        //友盟
        UMConfigure.setLogEnabled(true);
        UMConfigure.init(this,Constants.U_MENG_KEY,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setWeixin(Constants.WX_APP_ID, Constants.WX_APP_SIGN);
        PlatformConfig.setQQZone(Constants.QQ_APP_ID, Constants.QQ_APP_KEY);

        TbsDownloader.needDownload(getApplicationContext(), false);
        if (!QbSdk.isTbsCoreInited()) {
            QbSdk.preInit(getApplicationContext(), null);// 设置X5初始化完成的回调接口
        }

        //初始化X5内核
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                //x5内核初始化完成回调接口，此接口回调并表示已经加载起来了x5，有可能特殊情况下x5内核加载失败，切换到系统内核。
            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.e("****@@@@****","加载内核是否成功:"+b);
            }
        });
        RPSDK.initialize(context);
    }

    private void initOKHttp() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15000, TimeUnit.MILLISECONDS)
                .readTimeout(15000, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }
}
